package com.bk.service.empInfo;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.client.RestTemplate;

import com.bk.entity.EmployeeInfo;
import com.bk.entity.RoleInfo;
import com.bk.mapper.empInfo.EmpRemoteMapper;
import com.bk.mapper.empInfo.EmployeeInfoMapper;
import com.bk.service.RedisService;
import com.bk.util.GeneratorRandomUtil;
import com.bk.util.Md5Util;
import com.bk.util.ReturnUtil;
import com.bk.util.SerilizableObjectUtil;
import com.bk.util.TimeUtil;
import com.bk.vo.EmployeeRoleVo;

@Service
/**@author czl 获得柜员信息*/
@Transactional
public class EmployeeInfoService {
	
	@Autowired
	private EmployeeInfoMapper employeeInfoMapper;
	
	@Resource
	private EmpRemoteMapper empRemoteMapper;

	@Autowired
	private RestTemplate restTemplate;
	
	@Autowired
	private RedisService redisService;
	
	
	/**获取相应权限的柜员信息*/
	public EmployeeInfo getEmployeeInfo(String empId,String password,String authorityId) {
		String value=(String) redisService.getKey("EmployeeList");
		EmployeeInfo emp=new EmployeeInfo();
		if(value!=null) {
			emp=(EmployeeInfo) SerilizableObjectUtil.unserizlize(value);
			return emp;
		}
		emp=employeeInfoMapper.getEmployeeInfo(empId, Md5Util.getMd5(password),authorityId);
		redisService.setKey("getEmployeeInfo",SerilizableObjectUtil.serialize(emp));
		return emp;
	}

	/**获取所有的柜员信息*/
	public ReturnUtil<EmployeeInfo> getEmployeeList() {
		String value=(String) redisService.getKey("EmployeeList");
		List<EmployeeInfo> le=new ArrayList<EmployeeInfo>();
		if(value!=null) {
			le=(List<EmployeeInfo>) SerilizableObjectUtil.unserizlize(value);
			return new ReturnUtil<EmployeeInfo>(200,"柜员信息集合",le);
		}else {
			le=employeeInfoMapper.getEmployeeList();
			redisService.setKey("getEmployeeList",SerilizableObjectUtil.serialize(le));
			return le.size()>0?new ReturnUtil<EmployeeInfo>(200,"柜员信息集合",le):new ReturnUtil<EmployeeInfo>(400,"没有柜员信息",null);
		}
	}
	
	/**编辑或增加柜员信息*/
	public ReturnUtil<EmployeeRoleVo> editOrAddEmployeeInfo(EmployeeRoleVo employeeRoleVo,int flag) {
		List<EmployeeRoleVo> le=new ArrayList<EmployeeRoleVo>();
		String password = employeeRoleVo.getEmployeeInfo().getPassword();
		employeeRoleVo.getEmployeeInfo().setPassword(Md5Util.getMd5(password));
		if(flag==1) {//添加
			employeeRoleVo.getEmployeeInfo().setEmployeeId(GeneratorRandomUtil.getRandom(12));
			employeeRoleVo.getEmployeeInfo().setStartTime(TimeUtil.getCurrentTimeUtil());
			int returnCode=employeeInfoMapper.addEmployeeInfo(employeeRoleVo);
			le.add(employeeRoleVo);
			return returnCode>0 ? new ReturnUtil<EmployeeRoleVo>(200,"柜员信息添加成功",le):new ReturnUtil<EmployeeRoleVo>(400,"柜员信息添加失败",null);
		}else {//编辑
			int returnCode=employeeInfoMapper.editEmployeeInfo(employeeRoleVo);
			le.add(employeeRoleVo);
			return returnCode>0 ? new ReturnUtil<EmployeeRoleVo>(200,"柜员信息修改成功",le):new ReturnUtil<EmployeeRoleVo>(400,"柜员信息修改失败",null);
		}
		
	}

	/**删除柜员信息*/
	public ReturnUtil<Integer> deleteEmployeeInfoByEmpId(String empId) {
		int ReturnCode=employeeInfoMapper.deleteEmployeeInfoByEmpId(empId);
		List<Integer> arrayList = new ArrayList<Integer>();
		arrayList.add(ReturnCode); 
		if(ReturnCode>0) {
			return new ReturnUtil<Integer>(200,"柜员信息删除成功",arrayList);
		}else {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return new ReturnUtil<Integer>(400,"柜员信息删除失败",null);
		}
	}

	/**根据柜员名模糊查询柜员*/ 
	public ReturnUtil<EmployeeRoleVo> queryEmpInfoByEmpName(String empName) {
		List<EmployeeRoleVo> le=employeeInfoMapper.queryEmpInfoByEmpName(empName);
		return le.size()>0?new ReturnUtil<EmployeeRoleVo>(200,"模糊查询柜员信息成功",le):new ReturnUtil<EmployeeRoleVo>(400,"没有到查询柜员信息",null);
	}

	/**获取角色和柜员*/
	public ReturnUtil<EmployeeRoleVo> getEmployeeRoleVoList() {
		String value=(String) redisService.getKey("getEmployeeRoleVoList");
		List<EmployeeRoleVo> le=new ArrayList<EmployeeRoleVo>();
		if(value!=null) {
			le=(List<EmployeeRoleVo>) SerilizableObjectUtil.unserizlize(value);
			return new ReturnUtil<EmployeeRoleVo>(200,"柜员信息集合",le);
		}else {
			le=employeeInfoMapper.getEmployeeRoleVoList();
			redisService.setKey("getEmployeeRoleVoList", SerilizableObjectUtil.serialize(le));
			return le.size()>0?new ReturnUtil<EmployeeRoleVo>(200,"柜员信息集合",le):new ReturnUtil<EmployeeRoleVo>(400,"没有柜员信息",null);
		}
	}

	/**远程调用角色模块服务  通过权限id获取角色信息*/
	public RoleInfo getEmployeeRoleVoByAuthority(String authorityId) {
		ResponseEntity<RoleInfo> responseEntity=restTemplate.getForEntity("http://BK-ROLE/getRoleInfoByAuthority?authority={1}", RoleInfo.class,authorityId);
		//RoleInfo roleInfo=empRemoteMapper.getRoleInfoByAuthority(authorityId);
		return responseEntity!=null?responseEntity.getBody():null;
	}

	/**添加柜员*/
	public ReturnUtil<EmployeeRoleVo> addEmployeeInfo(String employeeName, String employeePass, String employeeSex,
			String empTelephone, int authorityId) {
		List<EmployeeRoleVo> le=new ArrayList<EmployeeRoleVo>();
		EmployeeInfo emp=new EmployeeInfo(GeneratorRandomUtil.getRandom(12),Md5Util.getMd5(employeePass), employeeName, employeeSex, empTelephone,TimeUtil.getCurrentTimeUtil() ,authorityId);
		EmployeeRoleVo employeeRoleVo=new EmployeeRoleVo();
		employeeRoleVo.setEmployeeInfo(emp);
		int returnCode=employeeInfoMapper.addEmployeeInfo(employeeRoleVo);
		le.add(employeeRoleVo);
		return returnCode>0 ? new ReturnUtil<EmployeeRoleVo>(200,"柜员信息添加成功",le):new ReturnUtil<EmployeeRoleVo>(400,"柜员信息添加失败",null);
	}

}
